package cn.edu.xhu.library_management.user.controller;

import cn.edu.xhu.library_management.common.entity.R;
import cn.edu.xhu.library_management.user.entity.LmUserStudent;
import cn.edu.xhu.library_management.user.entity.LmUserTeacher;
import cn.edu.xhu.library_management.user.entity.search.StudentSearch;
import cn.edu.xhu.library_management.user.entity.search.TeacherSearch;
import cn.edu.xhu.library_management.user.service.LmUserTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2023 - 04 - 18
 * @Description: 教师用户控制类
 */
@Api(tags = "教师管理模块")
@ApiSort(2)
@RestController
@RequestMapping("/lm_user/user/teacher")
public class UserTeacherController {

    @Autowired
    private LmUserTeacherService teacherService;
    @Autowired
    private BCryptPasswordEncoder encoder;

    @GetMapping("/search")
    @ApiOperation(value = "教师搜索", produces = "application/json")
    @ApiOperationSupport(order = 111)
    public R search(TeacherSearch search){
        Page<LmUserTeacher> teachers= teacherService.search(search);
        return R.ok().data("page", teachers);
    }

    @GetMapping("/{thId}")
    @ApiOperation(value = "通过教师ID查询教师", produces = "application/json")
    @ApiOperationSupport(order = 112)
    public R getStudentById(@PathVariable("thId") String thId){
        LmUserTeacher teacher = teacherService.getById(thId);
        return R.ok().data("teacher", teacher);
    }

    @PostMapping
    @ApiOperation(value = "添加教师用户", produces = "application/json")
    @ApiOperationSupport(order = 121)
    public R addUserStudent(@RequestBody LmUserTeacher userTeacher){
        String account = userTeacher.getThAccount();
        String initPwd = account.substring(account.length() - 6);
        // 加密 默认密码 后六位
        userTeacher.setThPwd(encoder.encode(initPwd));
        boolean flag = teacherService.save(userTeacher);
        return flag ? R.ok("添加用户成功") : R.error("添加用户失败");
    }

    @PutMapping
    @ApiOperation(value = "修改教师用户", produces = "application/json")
    @ApiOperationSupport(order = 131)
    public R updateUserStudent(@RequestBody LmUserTeacher userTeacher){
        boolean flag = teacherService.updateById(userTeacher);
        return flag ? R.ok("修改用户成功") : R.error("修改用户失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过Id删除用户", produces = "application/json")
    @ApiOperationSupport(order = 141)
    public R deleteUserStudent(@PathVariable("id") String id){
        boolean flag = teacherService.removeById(id);
        return flag ? R.ok("删除用户成功") : R.error("删除用户失败");
    }

    @DeleteMapping
    @ApiOperation(value = "删除用户(thIds)", produces = "application/json")
    @ApiOperationSupport(order = 142)
    public R deleteUserStudent(@RequestParam("thIds") List<String> thIds){
        boolean flag = teacherService.removeBatchByIds(thIds);
        return flag ? R.ok("批量删除用户成功") : R.error("批量删除用户失败");
    }
}
